External Entities

When we discussed the declaration of attributes and their types, one of the types that exists is ENTITY.  The ENTITY attribute type is used to reference an external points to an external 

You have now seen the referencing of an external binary entity by using an ENTITY attribute type.  You can reference non-binary entities in ENTITY attributes as well, but they will be parsed (and processed) by the XML processor.  Look at the following document:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE body [

    <!ENTITY xml.doc SYSTEM "External1.xml">

    <!ELEMENT body ANY>
    <!ELEMENT doc EMPTY>
        <!ATTLIST doc src ENTITY #REQUIRED>
]>
<body>
    <doc src="xml.doc"/>
</body>

File: Chapter 6\ExternalEntities.xml.

The first thing to point out is the <!ENTITY> declaration to an external XML document named “External1.xml”.

<!ENTITY xml.doc SYSTEM "External1.xml">

Next is the declaration of an ENTITY attribute associated with the <doc> element.  It is important to note that this attribute can point to any kind of external entity.

<!ELEMENT doc EMPTY>
    <!ATTLIST doc src ENTITY #REQUIRED>

Finally, look at the reference to the entity where it is assigned to the “src” attribute of an instance of the <doc> element.

<doc src="xml.doc"/>

The biggest point that needs to be made is that the entity is referenced not by its physical storage (file) name, but by its internal name as specified in the <!ENTITY> declaration.

Now, let’s look at the contents of the external entity.

<?xml version="1.0"?>
<body>
	<text>This is some text</text>
</body>

File: Chapter 6\External1.xml

The external entity will be parsed by the processor when it encounters the ENTITY attribute that references it.  Any errors that exist in the external document (malformed tags, missing quotes in assignments, etc.).  However, it’s contents will not be validated against the DTD of the master document.
Let’s look out our master document in IE5.

File: Chapter 6\ExternalEntities.xml in IE5.

Referencing external parsed entities in an attribute is rather straightforward.  Declare the entity, declare the attribute, the reference the entity within the attribute.  Now let’s look at declaring non-parsed and binary entities.